<?php

/**
 * Description of Question
 *
 * @author hoangle
 */
class Question {

    private $db;
    
    public function  __construct() {        
        $this->db = Zend_Registry::get('db');
    }
    /**
     * @desc Get Host list question
     * @return <array> data question
     */
    public function getHotQuestion($cate_id=NULL) {
        try {
            $sql  = "SELECT q.* FROM question q ";
            $sql .= "INNER JOIN category c ON c.id = q.cate_id ";
            $sql .= "WHERE hot_point>0 ";
            if($cate_id){
                $sql.= "AND c.id = ".$cate_id." ";
            }
            $sql.= "ORDER by hot_point LIMIT 5 ";
            $stmt = $this->db->prepare($sql);
            $stmt->execute();
            $result = $stmt->fetchAll();
            $stmt->closeCursor();
            unset($stmt);
        }
        catch (Zend_Db_Exception $e) {
            throw new Zend_Exception($e->getMessage(), 11);
        }
        return $result;
    }
    /**
     * @desc Get Host list question
     * @return <array> data question
     */
    public function getOpenQuestion($cate_id=NULL) {
        try {
            $sql  = "SELECT q.* FROM question q ";            
            $sql .= "INNER JOIN category c ON c.id = q.cate_id ";
            $sql .= "WHERE is_open=1 ";
            if($cate_id){
                $sql.= "AND c.id = ".$cate_id." ";
            }
            $sql.= "ORDER by date_ask LIMIT 5 ";
            $stmt = $this->db->prepare($sql);
            $stmt->execute();
            $result = $stmt->fetchAll();
            $stmt->closeCursor();
            unset($stmt);
        }
        catch (Zend_Db_Exception $e) {
            throw new Zend_Exception($e->getMessage(), 11);
        }
        return $result;
    }
    /**
     * @desc Insert question
     * @return <array> data question
     */
    public function insertQuestion($arrInput) {
        try {
            $sql  = "INSERT INTO question ";
            $sql .= "(quest_title, quest_content, cate_id, user_pick, is_open, date_ask, prize_give)";
            $sql .= " VALUES( '".$arrInput['quest_title']."','".$arrInput['quest_content']."','".$arrInput['cate_id']."','".$arrInput['user_pick']."','".$arrInput['is_open']."','".$arrInput['date_ask']."','".$arrInput['prize_give']."') ";
            
            $stmt = $this->db->prepare($sql);
            $stmt->execute();
            $result = $stmt->fetchAll();
            $stmt->closeCursor();
            unset($stmt);
        }
        catch (Zend_Db_Exception $e) {
            throw new Zend_Exception($e->getMessage(), 11);
        }
        return $result;
    }
    /**
     * @desc Get question hot by fromdate & todate
     * @param <array> $arrParam (fromdate, todate, offset, limit)
     * @return <array> if success else null or failed
     */
    public function getQuestionOutHome($arrParam) {
        try {
            $sql = "SELECT * FROM question WHERE date_ask >= :p_fromdate AND 
                date_ask <= :p_todate ORDER by hot_point limit :p_offset,:p_limit";
            $stmt = $this->db->prepare($sql);
            $stmt->bindParam(':p_fromdate', $arrParam['fromdate'], PDO::PARAM_STR);
            $stmt->bindParam(':p_todate', $arrParam['todate'], PDO::PARAM_STR);
            $stmt->bindParam(':p_offset', $arrParam['offset'], PDO::PARAM_INT);
            $stmt->bindParam(':p_limit', $arrParam['limit'], PDO::PARAM_INT);
            $stmt->execute();
            $result = $stmt->fetchAll();
            $stmt->closeCursor();
            unset($stmt);
        }
        catch (Zend_Db_Exception $e) {
            throw new Zend_Exception($e->getMessage(), 11);
        }
        return $result;
    }


    
}

?>
